package cn.wolfcode.education.service.impl;

import cn.wolfcode.education.domain.Menu;
import cn.wolfcode.education.mapper.MenuMapper;
import cn.wolfcode.education.query.QueryObject;
import cn.wolfcode.education.service.IMenuService;
import cn.wolfcode.education.util.MenuUtil;
import cn.wolfcode.education.util.PageResult;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MenuServiceImpl implements IMenuService {
    @Autowired
    private MenuMapper menuMapper;


    @Override
    public PageResult query(QueryObject qo) {
        int total = menuMapper.queryForCount(qo);
        if (total == 0) {
            return new PageResult();
        }
        List list = menuMapper.queryForList(qo);
        return new PageResult(total, list);

    }

    @Override
    public List<Menu> selectAllParent() {
        return menuMapper.selectAllParent();
    }

    @Override
    public List<Long> listIdsByEmployeeId(Long employeeId) {
        return menuMapper.listIdsByEmployeeId();
    }

    /**
     * 根据角色的id查询菜单的id集合
     *
     * @param roleId
     * @return
     */
    @Override
    public List<Long> listIdsByRoleId(Long roleId) {
        return menuMapper.listIdsByRoleId(roleId);
    }

    /**
     * 当前点登陆对象的相息存放在session中
     * 首页菜单  根据当前登陆对象去查属于当前对象的菜单
     * @return
     */
    public List<Menu> indexMenus() {
        List<Menu> menus = (List<Menu>) SecurityUtils.getSubject().getSession()
                .getAttribute(MenuUtil.MENUS_IN_SESSION);
        return menus;
    }
}
